﻿using Autofac;
using KJSyncXP.Access.Context;
using KJSyncXP.Oracle.Context;
using KJSyncXP.Sqlite.Model;
using KJSyncXP.Sqlite.ViewModels;
using KJSyncXP.SQLite.Context;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;

namespace KJSyncXP.Services
{
    public class AuthorizationService
    {

        public ObservableCollection<Permission> listPermission()
        {
            var scope = App._container.BeginLifetimeScope();
            var sqliteCtx = scope.Resolve<SqliteDbContext>();
            var result = sqliteCtx.Db.Queryable<Permission>().Where(x => x.Isshow == true).OrderBy(x => x.Ptype).ToList();
            return new ObservableCollection<Permission>(result);
        }

        public ObservableCollection<User> listUser(string deptName)
        {
            var scope = App._container.BeginLifetimeScope();
            var sqliteCtx = scope.Resolve<SqliteDbContext>();
            var result = sqliteCtx.Db.Queryable<User>().Where(x => x.Deptname == deptName).ToList();
            return new ObservableCollection<User>(result);
        }

        public ObservableCollection<Up> listUp(string useruuid)
        {
            var scope = App._container.BeginLifetimeScope();
            var sqliteCtx = scope.Resolve<SqliteDbContext>();
            var result = sqliteCtx.Db.Queryable<Up>().Where(x => x.Useruuid == useruuid).ToList();
            return new ObservableCollection<Up>(result);
        }


        public void SaveUser(User user)
        {
            var scope = App._container.BeginLifetimeScope();
            var sqliteCtx = scope.Resolve<SqliteDbContext>();
            if (user.id == 0)
            {
                sqliteCtx.Db.Insert<User>(user).ExecuteAffrows();
            }
            else
            {
                sqliteCtx.Db.Update<User>(user).ExecuteAffrows();
            }
        }

        public void AddUp(string pname, string puuid, string ptype,string uuuid, string uname)
        {
            var scope = App._container.BeginLifetimeScope();
            var sqliteCtx = scope.Resolve<SqliteDbContext>();
            Up up = new Up()
            {
                Pname = pname,
                Username = uname,
                Puuid = puuid,
                Ptype = ptype,
                Useruuid = uuuid,
                Atype = 1
            };
            sqliteCtx.Db.Insert<Up>(up).ExecuteAffrows();
        }

        public void RemoveUser(User user)
        {
            var scope = App._container.BeginLifetimeScope();
            var sqliteCtx = scope.Resolve<SqliteDbContext>();

            sqliteCtx.Db.Transaction(() =>
            {
                sqliteCtx.Db.Delete<Up>().Where(x => x.Useruuid == user.Useruuid).ExecuteAffrows();
                sqliteCtx.Db.Delete<User>(user).ExecuteAffrows();
            });
         
        }


        public void RemoveUp(string puuid, string uuuid)
        {
            var scope = App._container.BeginLifetimeScope();
            var sqliteCtx = scope.Resolve<SqliteDbContext>();

            sqliteCtx.Db.Delete<Up>().Where(x => x.Useruuid == uuuid && x.Puuid == puuid).ExecuteAffrows();
        }

        public List<VUserpermission> GetPermissions(string usruuid)
        {
            var scope = App._container.BeginLifetimeScope();
            var sqliteCtx = scope.Resolve<SqliteDbContext>();

            return sqliteCtx.Db.Queryable<VUserpermission>().Where(x => x.Useruuid == usruuid).NoTracking().ToList();
        }




        /*
        private const string moduleName = "授权";
       */
    }
}
